<template>
	<view class="qiun-columns">
		<view class="qiun-charts" >
			<!--#ifndef MP-ALIPAY -->
			<canvas canvas-id="canvasPie" id="canvasPie" class="charts" @tap="touchPie"></canvas>
			<!--#endif-->
		</view>
	</view>
</template>

<script>
	// import uCharts from '../../utils/uCharts/u-charts.min.js';
	import uCharts from '../../js_sdk/u-charts/u-charts.min.js';
	var _self;
	var canvaPie=null;
   
	export default {
		name:'pie-canvas',
		props: {
			pieJson:{
				type:Object,
				default:null
			},
			chartType:{
				type:String,
				default:'ring'
			}
		},
		data() {
			return {
				cWidth:'',
				cHeight:'',
				pixelRatio:1,
				textarea:'',
				colors:[]
			}
		},
		created() {
			_self = this;
			//#ifdef MP-ALIPAY
			uni.getSystemInfo({
				success: function (res) {
					if(res.pixelRatio>1){
						_self.pixelRatio =2;
					}
				}
			});
			//#endif
			this.cWidth=uni.upx2px(750);
			this.cHeight=uni.upx2px(500);
			this.pieJson.series.map(x=>{
				this.colors.push(x.color);
			})
			this.getServerData();
		},
		methods: {
			getServerData(){
				let Pie={series:[]};
				Pie.series=this.pieJson.series;
				_self.textarea = JSON.stringify(this.pieJson);
				_self.showPie("canvasPie",this.pieJson);
			},
			showPie(canvasId,chartData){
				canvaPie=new uCharts({
					$this:_self,
					canvasId: canvasId,
					type: this.chartType,
					fontSize:11,
					padding:[15,60,0,15],
					legend:{
						show:true,
						padding:5,
						lineHeight:20, //更改底部文字的间距
						margin:0,
					},
					background:'#FFFFFF',
					pixelRatio:_self.pixelRatio,
					series: chartData.series,
					animation: true,
					width: _self.cWidth*_self.pixelRatio,
					height: _self.cHeight*_self.pixelRatio,
					dataLabel: true,
					extra: {
						pie: {
						  border:true,
						  borderColor:'#FFFFFF',
						  borderWidth:1
						},
						rose:{
							labelWidth:30
						}
					},
				});
			},
			touchPie(e){
				canvaPie.showToolTip(e, {
					format: function (item) {
						return item.name + ':' + item.data 
					}
				});
				// canvaPie.touchLegend(e,{animation:true});
			},
			changeData(){
				if(this.$Common.isJSON(_self.textarea)){
					let newdata=JSON.parse(_self.textarea);
					canvaPie.updateData({
						series: newdata.series,
						categories: newdata.categories
					});
				}else{
					uni.showToast({
						title:'数据格式错误',
						image:'../../../static/images/alert-warning.png'
					})
				}
			}
		}
	}
</script>

<style>
	/*样式的width和height一定要与定义的cWidth和cHeight相对应*/
	.qiun-charts {
		width: 100%;
		height: 500upx;
		background-color: #FFFFFF;
	}
	
	.charts {
		width: 100%;
		height: 500upx;
		background-color: #FFFFFF;
	}
</style>
